<template>
  <view class="container">
    <view class="page-banner">
      <view class="grid-body">
        <view class="type">{{ currentExam.exercisesTypeLabel }}</view>
        <view class="content">{{ currentExam.examinationContent }}</view>

        <view
          v-for="(item, index) in currentExam.answerList"
          :key="index"
          class="option"
          :class="{ active: isCorrect(item), error: isError(item) }"
        >
          <text>{{ item.options }}.{{ item.answerContent }}</text>
          <image v-if="isCorrect(item)" src="/static/images/online-examination/select.png"></image>
          <image v-if="isError(item)" src="/static/images/online-examination/error.png"></image>
        </view>

        <view class="answer flex-space-between">
          <view>正确答案：<text class="correct">{{ getOptions(currentExam.correctOptionIdList) }}</text></view>
          <view>已选答案：<text class="error">{{ getOptions(currentExam.choseOptionIdList) }}</text></view>
        </view>
        <view class="analys">
          解析：{{currentExam.examinationAnalysis}}
        </view>

      </view>
    </view>
    <view class="page-footer flex-space-between">
      <view class="answer-sheet" @click="handlePopup">
        <image src="/static/images/online-examination/answer-sheet.png"></image>
        答题卡
      </view>
      <view class="btn-group">
        <view class="big-btn preview" @click="handlePreview">上一题</view>
        <view class="big-btn next" @click="handleNext">下一题</view>
      </view>
    </view>
  </view>
</template>

<script>
import { reqGetStart } from '@/api/educational-training/online-examination'
import { get, includes } from 'lodash'
import { mapMutations, mapState, mapActions } from 'vuex'
import { reqGetDetail, mockTestRecordDetail } from '@/api/educational-training/online-examination'

export default {
  name: 'examination-detail',

  props: {},

  data() {
    return {
      examPlanId: '',
			simulationId: '', // 模拟考试记录id
      testPaperData: {},
    }
  },

  computed: {
    ...mapState({
      currentPaperIndex: (state) => state.onlineExam.currentPaperIndex
    }),
    currentExam() {
      return this.paperList[this.currentPaperIndex] || {}
    },
    paperList() {
      return [
        ...get(this.testPaperData, 'singleList', []),
        ...get(this.testPaperData, 'multipleList', []),
        ...get(this.testPaperData, 'judgementList', [])
      ]
    },
    paperTotal() {
      return get(this.paperList, 'length', 0)
    },
    percent() {
      return Math.floor(this.currentPaperIndex / this.paperTotal) * 100
    }
  },

  watch: {},

  created() {},

  onLoad({ simulationId }) {
    this.simulationId = simulationId
    this.clearStore()
    this.initData()
  },

  destroyed() {},

  methods: {
    ...mapMutations(['setCurrentPaperIndex', 'clearStore']),
    async initData() {
			let res = await mockTestRecordDetail(this.simulationId)
      this.testPaperData = res.data
			uni.setNavigationBarTitle({ title: this.testPaperData.examinationTypeName + '-模拟考试' })
    },
    handlePreview() {
      if (this.currentPaperIndex > 0) {
        this.setCurrentPaperIndex(this.currentPaperIndex - 1)
      }
    },
    handleNext() {
      if (this.currentPaperIndex + 1 < this.paperTotal) {
        this.setCurrentPaperIndex(this.currentPaperIndex + 1)
      }
    },
    isCorrect({ answerId }) {
      return includes(this.currentExam.correctOptionIdList, answerId.toString())
    },
    isError({ answerId }) {
      return !this.isCorrect({ answerId }) && includes(this.currentExam.choseOptionIdList, answerId.toString())
    },
    getOptions(list = []) {
      return list.map(
        id => get(this.currentExam.answerList.find(item => item.answerId == id), 'options')
      ).join(',')
    },
    handlePopup() {
      uni.navigateTo({ url: `/pages/work/online-examination/answer-sheet?simulationId=${this.simulationId}&mode=view` })
    },
  }
}
</script>

<style>
page {
  height: 100%;
}
</style>
<style scoped lang="scss" type="text/scss">
.container {
  position: relative;
  height: 100%;
  background-color: #88b4c4;
  overflow-y: auto;
  padding-bottom: 180rpx;
}
.page-banner {
  padding: 0 30rpx;
  .grid-body {
    margin: 30rpx 0 0;
    padding: 30rpx;
    color: #333333;

    .type {
      width: 100rpx;
      height: 50rpx;
      line-height: 50rpx;
      background: #88b4c4;
      border-radius: 10rpx 10rpx 10rpx 10rpx;
      color: #fff;
      font-size: 12px;
      text-align: center;
    }

    .content {
      margin-top: 30rpx;
      margin-bottom: 40rpx;
      font-size: 30rpx;
      font-weight: bold;
      word-break: break-all;
    }

    .option {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding:30rpx 30rpx 30rpx 20rpx;
      width: 100%;
      //height: 88rpx;
      background: #f5f5f5;
      border-radius: 10rpx 10rpx 10rpx 10rpx;
      margin-bottom: 20rpx;
      font-size: 30rpx;

      & text{
        display: inline-block;
        min-height: 48rpx;
        line-height: 48rpx;
		    word-break: break-all;
      }
      
      &.active {
        background: #ebf2f5;
      }
      &.error {
        background: #efdedb;
      }
      image {
        flex-shrink: 0;
        width: 48rpx;
        height: 48rpx;
      }
    }
    .btn-group {
      margin-top: 40rpx;
    }
  }
}
.page-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 158rpx;
  background-color: #fff;
  padding-right: 30rpx;
  padding-left: 46rpx;

  .answer-sheet image {
    display: block;
    margin: 0 auto 5rpx;
    width: 54rpx;
    height: 54rpx;
  }
  .preview {
    width: 231rpx;
    color: #88b4c4;
    background: #fff;
  }
  .next {
    margin-left: 30rpx;
    width: 231rpx;
    color: #fff;
    background: #88b4c4;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;

  .big-btn {
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    font-size: 34rpx;
    text-align: center;
    height: 88rpx;
    line-height: 88rpx;
  }
}
.answer {
  font-size: 30rpx;
  .correct {
    font-size: 40rpx;
    color: #88B4C4;
    font-weight: bold;
  }
  .error {
    font-size: 40rpx;
    color: #A14430;
    font-weight: bold;
  }
}
.analys {
  color: #999999;
  font-size: 14px;
  margin-top: 20rpx;
  word-break: break-all;
}
</style>
